using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Starznet.SharePoint.SPDAT.ApplicationPages.Settings
{
    public class ManageEventHandlersPage : StarznetBaseLayoutPage
    {
        protected SPGridView GvListHandlers;
        protected HyperLink HlAddEditHandler;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            GvListHandlers.RowDataBound += new GridViewRowEventHandler(GvListHandlers_RowDataBound);

            if (!string.IsNullOrEmpty(Request.QueryString.Get("List")))
            {
                SPList oList = SPContext.Current.Web.Lists[new Guid(Request.QueryString.Get("List"))];
                this.GvListHandlers.DataSource = GetHandlers(oList);
            }
            else
            {
                this.GvListHandlers.DataSource = GetHandlers();
            }
            this.GvListHandlers.DataBind();

            HlAddEditHandler.NavigateUrl = "AddEditHandler.aspx";
            if(!string.IsNullOrEmpty(Request.QueryString.Get("List"))) HlAddEditHandler.NavigateUrl += "?List=" + Request.QueryString.Get("List");
        }

        void GvListHandlers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView oDrv = (DataRowView)e.Row.DataItem;

                HyperLink oLnk = (HyperLink)e.Row.FindControl("HlAddEdit");
                oLnk.NavigateUrl = "AddEditHandler.aspx?Handler=" + oDrv["GUID"].ToString();
                if (!string.IsNullOrEmpty(Request.QueryString.Get("List"))) oLnk.NavigateUrl += "&List=" + Request.QueryString.Get("List");

                oLnk.Text = oDrv["EventType"].ToString();
            }
        }

        private DataTable GetHandlers(SPList List)
        {
            DataTable oTable = new DataTable();
            oTable.Columns.Add(new DataColumn("EventType"));
            oTable.Columns.Add(new DataColumn("Assembly"));
            oTable.Columns.Add(new DataColumn("Class"));
            oTable.Columns.Add(new DataColumn("GUID"));
            oTable.Columns.Add(new DataColumn("ListID"));

            DataRow oRow = null;
            foreach (SPEventReceiverDefinition oEvent in List.EventReceivers)
            {
                oRow = oTable.NewRow();
                oRow["EventType"] = oEvent.Type.ToString();
                oRow["Assembly"] = oEvent.Assembly;
                oRow["Class"] = oEvent.Class;
                oRow["GUID"] = oEvent.Id.ToString();
                oRow["ListID"] = List.ID.ToString();

                oTable.Rows.Add(oRow);
            }
            return oTable;
        }

        private DataTable GetHandlers()
        {
            DataTable oTable = new DataTable();
            oTable.Columns.Add(new DataColumn("EventType"));
            oTable.Columns.Add(new DataColumn("Assembly"));
            oTable.Columns.Add(new DataColumn("Class"));
            oTable.Columns.Add(new DataColumn("GUID"));
            DataRow oRow = null;
            foreach (SPEventReceiverDefinition oEvent in SPContext.Current.Web.EventReceivers)
            {
                oRow = oTable.NewRow();
                oRow["EventType"] = oEvent.Type.ToString();
                oRow["Assembly"] = oEvent.Assembly;
                oRow["Class"] = oEvent.Class;
                oRow["GUID"] = oEvent.Id.ToString();
                
                oTable.Rows.Add(oRow);
            }
            return oTable;
        }
    }
}
